package com.bs.system.config;

import cn.hutool.core.collection.CollUtil;
import com.bs.common.constant.AuthConstant;
import com.bs.system.po.InterfaceDetails;
import com.bs.system.service.InterfaceDetailsService;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author bingshao
 * @date 2023/2/27
 **/
@Configuration
public class RoleUserConfig {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private InterfaceDetailsService interfaceDetailsService;

    /**
     * 将需要鉴权的接口url放入redis，在gateway处进行验证
     */
    @PostConstruct
    public void initRoleUser() {
        List<InterfaceDetails> interfaceDetailsList = interfaceDetailsService.list();
        if (CollUtil.isNotEmpty(interfaceDetailsList)) {
            interfaceDetailsList.forEach(interfaceDetails ->
                    stringRedisTemplate.opsForValue().setIfAbsent(AuthConstant.USER_ROLE + interfaceDetails.getUrl(), interfaceDetails.getRoleIds()));
        }
    }
}
